<?php
/**
 * Created by PhpStorm.
 * UsersF: Mloong
 * Date: 2018/12/7
 * Time: 10:28
 */

namespace app\login\controller;


use app\api\service\Token;
use app\lib\exception\MysqlErrorException;
use app\login\model\Account;
use app\login\model\Popularize;
use app\login\model\Record;
use app\login\service\RecordService;
use app\login\service\ShowService;
use app\login\validate\PageValidate;
use app\login\validate\ShowValidate;
use think\facade\Request;

class Show extends BaseController
{
    public function index()
    {
        return $this->fetch('/login/show');
    }

    # 根据token里面的user获取所有记录数
    public function getByFrom($page,$limit,$model)
    {
        (new ShowValidate())->goCheck();
        (new PageValidate())->goCheck();
        $name = Token::getRedisTokenName();
        $result = RecordService::getFromName($name,$page,$limit,$model);
        if(!$result){
            throw new MysqlErrorException();
        }
        return json([
            'code' => 0,
            'data'=> $result['data'],
            'count' => $result['count']
        ]);
    }

    # 根据token里面的user获取username和标识码
    public function getUserData()
    {
        $name = Token::getRedisTokenName();
        $model = new ShowService();
        $arr = $model->getSign($name);
        return json([
            'name' => $name,
            'sign' => $arr['arr'],
            'host_d' => $arr['host_d'],
            'host_f' => $arr['host_f'],
        ]);
    }

    # 按条件查询接口
    public function byCondition($model)
    {
        (new ShowValidate())->goCheck();
        $name = Token::getRedisTokenName();
        $post = Request::param();
        $result = RecordService::getByCondition($name,$post,$model);
        return json([
            'code' => 0,
            'data'=> $result['data'],
            'count' => $result['count']
        ]);
    }

}